<?php

class SMCore_Utilities_Filter {
	
	// Case 1
	public static function fiterColorScale($img, $imgPath, $style) {
		$fileorginal = $imgPath;
		$fileinfo = pathinfo ( $fileorginal ); //print_r($fileinfo);exit();
		$fileExtension = ($fileinfo ['extension']);
		$filename = $fileinfo ['basename'];
		$fullfilename = $fileinfo ['filename'];
		$path = $fileinfo ['dirname'];
		try {
			switch (strtolower ( $fileExtension )) {
				case "jpeg" :
				case "jpg" :
					$im = imagecreatefromjpeg ( $path . DS . $filename );
					//header ( 'Content-Type: image/jpg' );
					//imagefilter ( $im, IMG_FILTER_NEGATE );
					//imagefilter ( $im, IMG_FILTER_GRAYSCALE );
					imagefilter ( $im, IMG_FILTER_BRIGHTNESS, - 100 );
					imagefilter ( $im, IMG_FILTER_CONTRAST, 0 );
					if ($im && imagefilter ( $im, IMG_FILTER_COLORIZE, $style [0], $style [1], $style [2] )) {
						//echo 'Image converted.';
						$newIm = $fullfilename . '-' . time () . '.' . strtolower ( $fileExtension );
						imagejpeg ( $im, $path . DS . $newIm );
					} else {
						echo 'Conversion failed.';
					}
					imagedestroy ( $im );
					break;
				case "png" :
					$im = imagecreatefrompng ( $path . DS . $filename );
					//header ( 'Content-Type: image/png' );
					imagefilter ( $im, IMG_FILTER_BRIGHTNESS, - 100 );
					imagefilter ( $im, IMG_FILTER_CONTRAST, 0 );
					if ($im && imagefilter ( $im, IMG_FILTER_COLORIZE, $style [0], $style [1], $style [2] )) {
						//echo 'Image converted.';
						$newIm = $fullfilename . '-' . time () . '.' . strtolower ( $fileExtension );
						imagepng ( $im, $path . DS . $newIm );
					} else {
						echo 'Conversion failed.';
					}
					imagedestroy ( $im );
					break;
				case "gif" :
					$im = imagecreatefromgif ( $path . DS . $filename );
					//header ( 'Content-Type: image/gif' );
					imagefilter ( $im, IMG_FILTER_BRIGHTNESS, - 100 );
					imagefilter ( $im, IMG_FILTER_CONTRAST, 0 );
					if ($im && imagefilter ( $im, IMG_FILTER_COLORIZE, $style [0], $style [1], $style [2] )) {
						//echo 'Image converted .';
						$newIm = $fullfilename . '-' . time () . '.' . strtolower ( $fileExtension );
						imagegif ( $im, $path . DS . $newIm );
					} else {
						echo 'Conversion failed.';
					}
					imagedestroy ( $im );
					break;
				case "bmp" :
					$im = imagecreatefromwbmp ( $path . DS . $filename );
					//header ( 'Content-Type: image/bmp' );
					imagefilter ( $im, IMG_FILTER_BRIGHTNESS, - 100 );
					imagefilter ( $im, IMG_FILTER_CONTRAST, 0 );
					if ($im && imagefilter ( $im, IMG_FILTER_COLORIZE, $style [0], $style [1], $style [2] )) {
						//echo 'Image converted .';
						$newIm = $fullfilename . '-' . time () . '.' . strtolower ( $fileExtension );
						imagewbmp ( $im, $path . DS . $newIm );
					} else {
						echo 'Conversion failed.';
					}
					imagedestroy ( $im );
					break;
			}
			// Apply layer to image
			$fileNoLayer = $path . DS . $newIm;
			$fileLayer = BASE_PATH . DS . 'images/dark_transparent_bigsize.png';
			
			// Get image size
			list ( $width, $height ) = getimagesize ( $path . DS . $newIm );
			
			$imTrans = imagecreatetruecolor ( $width, $height );
			//$red = imagecolorallocate ( $imTrans, 255, 0, 0 );
			//$black = imagecolorallocate ( $imTrans, 0, 0, 0 );
			

			$black = imagecolorallocatealpha ( $imTrans, 0, 0, 0, 127 );
			// Make the background transparent
			imagecolortransparent ( $imTrans, $black );
			
			// Save the image
			$fileLayerTemp = BASE_PATH . DS . 'data' . DS . 'temp' . DS . 'images' . DS . 'dark.png';
			imagepng ( $imTrans, $fileLayerTemp );
			imagedestroy ( $imTrans );
			
			$first = new Imagick ( $fileNoLayer );
			$second = new Imagick ( $fileLayerTemp );
			
			$first->compositeImage ( $second, $second->getImageCompose (), 0, 0 );
			
			//new image is saved as layer...
			$first->writeImage ( $path . DS . 'layer' . $newIm );
			$layerIm = 'layer' . $newIm;
			
			return $layerIm;
		} catch ( Exception $e ) {
			echo $e->getMessage ();
		}
	}
	/**
	 * crop image
	 */
	public function cropImage($w, $h, $fileLayer) {
		$fileLayerTemp = $config->site->upload->dir->temp . DS . 'images' . DS . 'dark.png';
		$objImage = new SMCore_Utilities_Image_CropCanvas ();
		if ($objImage->loadImage ( $fileLayer )) {
			$objImage->cropToSize ( $w, $h, ccCENTER );
			$objImage->saveImage ( $fileLayerTemp );
			$objImage->flushImages ();
			return 'dark.png';
		} else {
			return 'error';
		}
	}

}
?>
